<template>
  <div>
    <el-form :model="ruleForm" status-icon :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="原密码" prop="oldPass">
        <el-input type="password" v-model="ruleForm.oldPass" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="新密码" prop="newPass1">
        <el-input type="password" v-model="ruleForm.newPass1" autocomplete="off"></el-input>
      </el-form-item>
      <el-form-item label="确认密码" prop="newPass2">
        <el-input type="password" v-model="ruleForm.newPass2" autocomplete="off"></el-input>
      </el-form-item>
    </el-form>
    <div class="btns" style="text-align:center;">
      <el-button type="primary" @click="submitForm('ruleForm')">提交</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </div>
  </div>
</template>

<script>
import md5 from 'md5';
import BetterUtilTools from 'better-util-tools';

const _ = new BetterUtilTools();
export default {
    'props':{
        'dialogVisible':{
            'type':Boolean,
            'default':()=>false
        }
    },
    data() {
        let oldPass = (rule, value, callback) => {
            if (!value) {
                return callback(new Error('原密码不能为空'));
            }
            callback();
        };
        let newPass1 = (rule, value, callback) => {
            if (!value) {
                callback(new Error('请输入新密码'));
            } else {
                if (this.ruleForm.newPass2 !== '') {
                    this.$refs.ruleForm.validateField('newPass2');
                }
                callback();
            }
        };
        let newPass2 = (rule, value, callback) => {
            if (!value) {
                callback(new Error('请确认密码'));
            } else if (value !== this.ruleForm.newPass1) {
                callback(new Error('两次输入密码不一致!'));
            } else {
                callback();
            }
        };
        return {
            'ruleForm': {
                'oldPass': '',
                'newPass1': '',
                'newPass2': ''
            },
            'rules': {
                'oldPass': [
                    { 'validator': oldPass, 'trigger': 'blur' }
                ],
                'newPass1': [
                    { 'validator': newPass1, 'trigger': 'blur' }
                ],
                'newPass2': [
                    { 'validator': newPass2, 'trigger': 'blur' }
                ]
            }
        };
    },
    'methods': {
        submitForm(formName) {
            this.$refs[formName].validate(valid => {
                if (valid) {
                    const tempForm = UtilTools.deepCopy({},this.ruleForm);
                    Object.keys(tempForm).forEach(k=>{
                        tempForm[k] = md5(tempForm[k]);
                    });
                    this.$emit('sure',tempForm);
                } else {
                    console.log('error submit!!');
                    return false;
                }
            });
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        }
    }
};
</script>

<style scoped>

</style>
